﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Cosmos.Database;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Configuration;

namespace CoastalClimbing
{
    public static class Common
    {
        public static class Constants
        {
            public static string INSTAGRAM_CLIENT_ID = ConfigurationManager.AppSettings["instagram_client_id"].ToString();
            public static string INSTAGRAM_CLIENT_SECRET = ConfigurationManager.AppSettings["instagram_client_secret"].ToString();
            public static string INSTAGRAM_REDIRECT_URI = ConfigurationManager.AppSettings["instagram_redirect_uri"].ToString();
            public static string INSTAGRAM_LOGIN = ConfigurationManager.AppSettings["instagram_login"].ToString();
            public static string INSTAGRAM_PASSWORD = ConfigurationManager.AppSettings["instagram_password"].ToString();
        }

        // Create a thumbnail in byte array format from the image encoded in the passed byte array.  
        // (RESIZE an image in a byte[] variable.)  
        public static byte[] CreateThumbnail(byte[] PassedImage, int LargestSide)
        {
            byte[] ReturnedThumbnail;

            using (MemoryStream StartMemoryStream = new MemoryStream(), NewMemoryStream = new MemoryStream())
            {
                // write the string to the stream  
                StartMemoryStream.Write(PassedImage, 0, PassedImage.Length);

                // create the start Bitmap from the MemoryStream that contains the image  
                Bitmap startBitmap = new Bitmap(StartMemoryStream);

                // set thumbnail height and width proportional to the original image.  
                int newHeight;
                int newWidth;
                double HW_ratio;
            
                if (startBitmap.Height > startBitmap.Width)
                {
                    newHeight = LargestSide;
                    HW_ratio = (double)((double)LargestSide / (double)startBitmap.Height);
                    newWidth = (int)(HW_ratio * (double)startBitmap.Width);
                }
                else
                {
                    newWidth = LargestSide;
                    HW_ratio = (double)((double)LargestSide / (double)startBitmap.Width);
                    newHeight = (int)(HW_ratio * (double)startBitmap.Height);
                }

                if (startBitmap.Height > newHeight || startBitmap.Width > newWidth)
                {
                    // create a new Bitmap with dimensions for the thumbnail.  
                    Bitmap newBitmap = new Bitmap(newWidth, newHeight);

                    // Copy the image from the START Bitmap into the NEW Bitmap.  
                    // This will create a thumnail size of the same image.  
                    newBitmap = ResizeImage(startBitmap, newWidth, newHeight);

                    // Save this image to the specified stream in the specified format.  
                    newBitmap.Save(NewMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                    // Fill the byte[] for the thumbnail from the new MemoryStream.  
                    ReturnedThumbnail = NewMemoryStream.ToArray();
                }
                else
                {
                    ReturnedThumbnail = PassedImage;
                }
            }

            // return the resized image as a string of bytes.  
            return ReturnedThumbnail;
        }

        // Resize a Bitmap  
        private static Bitmap ResizeImage(Bitmap image, int width, int height)
        {
            Bitmap resizedImage = new Bitmap(width, height);
            
            using (Graphics gfx = Graphics.FromImage(resizedImage))
            {
                gfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gfx.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
            }

            return resizedImage;
        } 
    }
}